/**
 * Created by lee_mxue on 17/2/16.
 */
import React from "react";
import ReactDOM from "react-dom";
import wx from "wx";
import AJAX from "../../lib/ajax";
import { addErrorListener } from "../../lib/handleError"
import weixinShare from "../../lib/weixinShare"
const Toast = require('../MyPackage/components/Toast')

require('../../lib/rem');
require('./PackageTracker.less');
require('../../styles/common.less');
require('../../styles/resetWeui.less');
require('../../styles/lux.less')


class App extends React.Component {
    constructor(props) {
        super(props);
        this.handleInputExpress = this.handleInputExpress.bind(this);   //输入快递公司
        this.handleScan = this.handleScan.bind(this);   //打开微信扫一扫
        this.handleInputPackage = this.handleInputPackage.bind(this);   //输入快递单号
        this.handleMatchExpress = this.handleMatchExpress.bind(this);   //根据快递单号识别快递公司
        this.handleOk = this.handleOk.bind(this);   //查询
        this.state = {
            express_info: undefined,    //已选快递公司
            input_express: "",  //输入快递公司
            input_package_id: undefined,   //输入快递单号

        }
    }

    componentWillMount() {
        addErrorListener()()
        weixinShare.hideAll();
    }

    componentDidMount() {
        AJAX.appendBaiduAnalysis()

        let express_info,input_package_id
        try{
            //获取localStorage－－－快递公司信息
            var checkPackage_express_info_x = JSON.parse(localStorage.getItem('checkPackage_express_info_x'));
            express_info = checkPackage_express_info_x
            //获取localStorage－－－快递单号
            var PackageTracker_package_id_x = JSON.parse(localStorage.getItem('PackageTracker_package_id_x'));
            input_package_id = PackageTracker_package_id_x
        }catch(e){
            
        }
        this.setState({
            express_info: checkPackage_express_info_x,
            input_package_id: PackageTracker_package_id_x,
        })
    }

    //toast弹窗
    warn(content) {
        Toast.warn({
            content: content
        })
    }

    //打开微信扫一扫
    handleScan() {
        var _this = this;
        AJAX.request("get_signature", {
            full_url: window.location.href
        }, (data) => {
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.body.appid, // 必填，公众号的唯一标识
                timestamp: data.body.timestamp, // 必填，生成签名的时间戳
                nonceStr: data.body.noncestr, // 必填，生成签名的随机串
                signature: data.body.signature,// 必填，签名，见附录1
                jsApiList: ["scanQRCode"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            })
            wx.ready(()=> {
                wx.scanQRCode({
                    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                    scanType: ["barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                    success: function (res) {
                        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                        var result_list = [];
                        result_list = result.split(",");
                        // alert(result_list[1])

                        // window.location.href = "MyPackageTracking?package_id=" + result_list[1]
                        _this.setState({
                            input_package_id: result_list[1]
                        })
                        // //将快递单号存入localstorage
                        // localStorage.removeItem("PackageTracker_package_id_x")
                        // localStorage.setItem("PackageTracker_package_id_x", result_list[1])
                        //    失焦－－－根据订单号匹配快递公司
                        _this.handleMatchExpress()
                    }
                });
            })
        })
    }

    //输入快递公司
    handleInputExpress(e) {
        this.setState({
            input_express: e.target.value

        })
    }

    //输入快递单号
    handleInputPackage(e) {
        var value = e.target.value
        this.setState({
            input_package_id: value
        })
    }

    //失焦－－－根据订单号匹配快递公司
    handleMatchExpress() {
        var _this = this;
        if (this.state.input_package_id) {
            //将快递单号存入localstorage
            localStorage.removeItem("PackageTracker_package_id_x")
            localStorage.setItem("PackageTracker_package_id_x", this.state.input_package_id)
            AJAX.request("express_match", {
                exp_code: this.state.input_package_id
            }, (data) => {
                if (data.code == 0) {
                    if (data.body.id != 0) {
                        var my_express_info = {
                            id: data.body.id,
                            name: data.body.name,
                            img: data.body.img,
                        }
                        // alert(my_express_info)
                        _this.setState({
                            express_info: my_express_info
                        })
                    }
                }
            })
        }
    }

    //查询
    handleOk() {
        if (!this.state.express_info) {
            this.warn("请选择快递公司")
        } else if (!this.state.input_package_id) {
            this.warn("请输入快递单号")
        } else {
            window.location.href = "MyPackageTracking?package_id=" + this.state.input_package_id + "&express_id=" + this.state.express_info.id
        }
    }

    render() {
        return (
            <div className="main">
                <div className="weui-cells">
                    <div className="weui-cell">
                        <div className="weui-cell__bd">
                            <p className="">
                                <input className="fontSize16px" value={this.state.input_package_id}
                                       onBlur={this.handleMatchExpress}
                                       onChange={this.handleInputPackage} type="text"
                                       placeholder="请输入快递单号" name="" id=""/>
                            </p>
                        </div>
                        <div className="weui-cell__ft" onClick={this.handleScan}>
                            <i className="iconfont  textBlue icon-saomiaoerweima"></i>
                        </div>
                    </div>

                    <div className="weui-cell">
                        <div className="weui-cell__bd">
                            <p className="">
                                <input type="text" onChange={this.handleInputExpress}
                                       value={this.state.express_info ? this.state.express_info.name : this.state.input_express}
                                       className="fontSize16px" placeholder="请选择快递公司" name="" id=""/>
                            </p>
                        </div>
                        <div className="weui-cell__ft" onClick={function () {
                            window.location.href = './ExpressCompany'
                        }}>
                            <i className="iconfont iconG icon-fangdajing"></i>
                        </div>
                    </div>
                </div>
                <div className="weui-btn-area yellow">
                    <a className="weui-btn yellow fontSize16px  weui-btn_primary" href="javascript:" id="showTooltips"
                       onClick={this.handleOk}>查询</a>
                </div>
            </div>
        )
    }
}

ReactDOM.render(<App />, document.getElementById('app'));
if (module.hot) {
    module.hot.accept();
}
